﻿using System;
using System.ComponentModel;
using System.Windows.Input;

namespace MyCalculator
{
    internal class Calculator_ViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }

        //Model实例化
        private Calculator_Model _CalculatorM = new Calculator_Model();

        #region 声明数据
        /// <summary>
        /// 声明Model的数据
        /// </summary>
        public double Num1
        {
            get { return _CalculatorM.Num1; }
            set
            {
                _CalculatorM.Num1 = value;
                RaisePropertyChanged("Num1");
            }
        }

        public double Num2
        {
            get { return _CalculatorM.Num2; }
            set
            {
                _CalculatorM.Num2 = value;
                RaisePropertyChanged("Num2");
            }
        }

        public string Flag1
        {
            get { return _CalculatorM.Flag1; }
            set
            {
                _CalculatorM.Flag1 = value;
                RaisePropertyChanged("Flag1");
            }
        }

        public string Flag2
        {
            get { return _CalculatorM.Flag2; }
            set
            {
                _CalculatorM.Flag2 = value;
                RaisePropertyChanged("Flag2");
            }
        }

        public string Str1
        {
            get { return _CalculatorM.Str1; }
            set
            {
                _CalculatorM.Str1 = value;
                RaisePropertyChanged("Str1");
            }
        }

        public string Str2
        {
            get { return _CalculatorM.Str2; }
            set
            {
                _CalculatorM.Str2 = value;
                RaisePropertyChanged("Str2");
            }
        }
        #endregion



        /// <summary>
        /// 按钮控件函数
        /// </summary>
        void Btn_Addnum0()
        {
            if (Str1 != null && Str1.Contains("="))
            {
                Str1 = null;
                Str2 = "0";
            }
            else
            {
                Str2 = Str2 + "0";
            }
        }

        void Btn_Addnum1()
        {
            if (Str1 != null && Str1.Contains("="))
            {
                Str1 = null;
                Str2 = "1";
            }
            else
            {
                Str2 = Str2 + "1";
            }
        }

        void Btn_Addnum2()
        {
            if (Str1 != null && Str1.Contains("="))
            {
                Str1 = null;
                Str2 = "2";
            }
            else
            {
                Str2 = Str2 + "2";
            }
        }

        void Btn_Addnum3()
        {
            if (Str1 != null && Str1.Contains("="))
            {
                Str1 = null;
                Str2 = "3";
            }
            else
            {
                Str2 = Str2 + "3";
            }
        }

        void Btn_Addnum4()
        {
            if (Str1 != null && Str1.Contains("="))
            {
                Str1 = null;
                Str2 = "4";
            }
            else
            {
                Str2 = Str2 + "4";
            }
        }

        void Btn_Addnum5()
        {
            if (Str1 != null && Str1.Contains("="))
            {
                Str1 = null;
                Str2 = "5";
            }
            else
            {
                Str2 = Str2 + "5";
            }
        }

        void Btn_Addnum6()
        {
            if (Str1 != null && Str1.Contains("="))
            {
                Str1 = null;
                Str2 = "6";
            }
            else
            {
                Str2 = Str2 + "6";
            }
        }

        void Btn_Addnum7()
        {
            if (Str1 != null && Str1.Contains("="))
            {
                Str1 = null;
                Str2 = "7";
            }
            else
            {
                Str2 = Str2 + "7";
            }
        }

        void Btn_Addnum8()
        {
            if (Str1 != null && Str1.Contains("="))
            {
                Str1 = null;
                Str2 = "8";
            }
            else
            {
                Str2 = Str2 + "8";
            }
        }

        void Btn_Addnum9()
        {
            if (Str1 != null && Str1.Contains("="))
            {
                Str1 = null;
                Str2 = "9";
            }
            else
            {
                Str2 = Str2 + "9";
            }
        }

        void Btn_AddDot()
        {
            if (Str2 == null)
            {
                return;
            }
            else if (!Str2.Contains("."))
            {
                Str2 = Str2 + ".";
            }
        }

        void Btn_Powx()
        {
            Flag2 = "pow";
            if (Str2 != null && Str1 == null)
            {
                Str1 = $"pow({Str2})=";
                Num1 = double.Parse(Str2);
                Str2 = Math.Pow(Num1, 2).ToString();
            }
            else if (Str2 != null && (!Str1.Contains(Flag2)))
            {
                Str1 = $"pow({Str2})=";
                Num1 = double.Parse(Str2);
                Str2 = Math.Pow(Num1, 2).ToString();
            }
        }

        void Btn_Sqrtx()
        {
            Flag2 = "sqrt";
            if (Str2 != null && Str1 == null)
            {
                Str1 = $"sqrt({Str2})=";
                Num1 = double.Parse(Str2);
                Str2 = Math.Pow(Num1, 0.5).ToString();
            }
            else if (Str2 != null && (!Str1.Contains(Flag2)))
            {
                Str1 = $"sqrt({Str2})=";
                Num1 = double.Parse(Str2);
                Str2 = Math.Pow(Num1, 0.5).ToString();
            }
        }

        void Btn_Clear()
        {
            Num1 = 0;
            Num2 = 0;
            Str1 = null;
            Str2 = null;
            Flag1 = null;
            Flag2 = null;
        }

        void Btn_Backspace()
        {
            if (Str2 != null && Str2.Length > 1)
            {
                Str2 = Str2.Substring(0, Str2.Length - 1);
            }
            else
            {
                Str2 = null;
            }
        }

        void Btn_Plus()
        {
            Flag1 = "+";
            if ((Str1 == null && Str2 != null) || Str1.Contains("="))
            {
                Str1 = Str2 + "+";
                Str2 = null;
            }
            else if (Str1.Contains("-") || Str1.Contains("×") || Str1.Contains("÷"))
            {
                Str1 = Str1.Substring(0, Str1.Length - 1);
                Str1 = Str1 + "+";
            }
        }

        void Btn_Sub()
        {
            Flag1 = "-";
            if ((Str1 == null && Str2 != null) || Str1.Contains("="))
            {
                Str1 = Str2 + "-";
                Str2 = null;
            }
            else if (Str1.Contains("+") || Str1.Contains("×") || Str1.Contains("÷"))
            {
                Str1 = Str1.Substring(0, Str1.Length - 1);
                Str1 = Str1 + "-";
            }
        }

        void Btn_Mul()
        {
            Flag1 = "×";
            if ((Str1 == null && Str2 != null) || Str1.Contains("="))
            {
                Str1 = Str2 + "×";
                Str2 = null;
            }
            else if (Str1.Contains("+") || Str1.Contains("-") || Str1.Contains("÷"))
            {
                Str1 = Str1.Substring(0, Str1.Length - 1);
                Str1 = Str1 + "×";
            }
        }

        void Btn_Div()
        {
            Flag1 = "÷";
            if ((Str1 == null && Str2 != null) || Str1.Contains("="))
            {
                Str1 = Str2 + "÷";
                Str2 = null;
            }
            else if (Str1.Contains("+") || Str1.Contains("-") || Str1.Contains("×"))
            {
                Str1 = Str1.Substring(0, Str1.Length - 1);
                Str1 = Str1 + "÷";
            }
        }

        void Btn_Equ()
        {
            if (Str1!=null&&Str2!=null&&(!Str1.Contains("=")))
            {
                if(Str1.Contains("+") || Str1.Contains("-") || Str1.Contains("×") || Str1.Contains("÷"))
                {
                    double ans;
                    Num1 = double.Parse(Str1.Substring(0, Str1.Length - 1));
                    Num2 = double.Parse(Str2);
                    switch (Str1.Substring(Str1.Length - 1, 1))
                    {
                        case "+": ans = Num1 + Num2;break;
                        case "-": ans = Num1 - Num2; break;
                        case "×": ans = Num1 * Num2; break;
                        case "÷": ans = Num1 / Num2; break;
                        default: return;
                    }
                    Str1 = Str1 + Str2 + "=";
                    Str2=ans.ToString();
                }
            }
        }

        bool CanLoginExecute()
        {
            return true;
        }

        public ICommand Addnum0 => new RelayCommond(Btn_Addnum0, CanLoginExecute);
        public ICommand Addnum1 => new RelayCommond(Btn_Addnum1, CanLoginExecute);
        public ICommand Addnum2 => new RelayCommond(Btn_Addnum2, CanLoginExecute);
        public ICommand Addnum3 => new RelayCommond(Btn_Addnum3, CanLoginExecute);
        public ICommand Addnum4 => new RelayCommond(Btn_Addnum4, CanLoginExecute);
        public ICommand Addnum5 => new RelayCommond(Btn_Addnum5, CanLoginExecute);
        public ICommand Addnum6 => new RelayCommond(Btn_Addnum6, CanLoginExecute);
        public ICommand Addnum7 => new RelayCommond(Btn_Addnum7, CanLoginExecute);
        public ICommand Addnum8 => new RelayCommond(Btn_Addnum8, CanLoginExecute);
        public ICommand Addnum9 => new RelayCommond(Btn_Addnum9, CanLoginExecute);

        public ICommand AddDot => new RelayCommond(Btn_AddDot, CanLoginExecute);
        public ICommand Powx => new RelayCommond(Btn_Powx, CanLoginExecute);
        public ICommand Sqrtx => new RelayCommond(Btn_Sqrtx, CanLoginExecute);
        public ICommand Clear => new RelayCommond(Btn_Clear, CanLoginExecute);
        public ICommand Backspace => new RelayCommond(Btn_Backspace, CanLoginExecute);
        public ICommand Plus => new RelayCommond(Btn_Plus, CanLoginExecute);
        public ICommand Sub => new RelayCommond(Btn_Sub, CanLoginExecute);
        public ICommand Mul => new RelayCommond(Btn_Mul, CanLoginExecute);
        public ICommand Div => new RelayCommond(Btn_Div, CanLoginExecute);
        public ICommand Equ => new RelayCommond(Btn_Equ, CanLoginExecute);
    }
}
